package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.Mapper.SpeechMapper;
import com.example.demo.base.*;
import com.example.demo.entity.Speech;
import com.example.demo.service.StanfordNlpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;

@RestController
@RequestMapping("/zutnlp")
public class SentenceController {

    @Resource
    SpeechMapper speechMapper;

    @Autowired
    private StanfordNlpService stanfordNlpService;

    //中文分词
    @ResponseBody
    @RequestMapping(value="/segmentation")
    public Result<?> Segmentation(@RequestBody String sentence) {
        String str = sentence;
        Segmentation segmentation = new Segmentation(sentence);
        String str2 = segmentation.getSegtext();
        return Result.success(str2);
    }

    //中文分句
    @ResponseBody
    @RequestMapping(value="/senSplit")
    public Result<?> SenSplit(@RequestBody String sentence) {
        ArrayList<String> sensRes = new SenSplit(sentence).getSensRes();
        String str1 = "";
        for(String str:sensRes){
            str1 = str1 + str + "\n";
        }
        return Result.success(str1);
    }

    //词性标注
    @ResponseBody
    @RequestMapping(value="/posTag")
    public Result<?> PosTag(@RequestBody String sentence) {
        String str = new PosTag(sentence).getPostext();
        return Result.success(str);
    }

    //命名实体识别
    @ResponseBody
    @RequestMapping(value="/namedEntity")
    public Result<?> NamedEntity(@RequestBody String sentence) {
        String str = sentence;
        String str2 = new NamedEntity(str).getNertext();
        return Result.success(str2);
    }


    //句子分析树
    @ResponseBody
    @RequestMapping(value="/spTree1")
    public Result<?> SPTree1(@RequestBody String sentence) {
        SPTree spTree = new SPTree(sentence);
        String str = spTree.getPrasetext();
        return Result.success(str);
    }

    //句子依存
    @ResponseBody
    @RequestMapping(value="/spTree2")
    public Result<?> SPTree2(@RequestBody String sentence) {
        SPTree spTree = new SPTree(sentence);
        String str = spTree.getDepprasetext();
        return Result.success(str);
    }

    //获取数据库数据
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<Speech> wrapper = Wrappers.<Speech>lambdaQuery();
        Page<Speech> speechPage = speechMapper.selectPage(new Page<>(pageNum, pageSize), wrapper );
        return Result.success(speechPage);
    }

    @PostMapping
    @RequestMapping("/dep")
    public DepData dep(@RequestParam("document") final String input){
        return stanfordNlpService.getDep(input);
    }
}
